package com.itboye.sunsun.person.ui;

import java.lang.ref.WeakReference;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsunhome.R;
import com.itboye.sunsun.MainHomeActivity;
import com.itboye.sunsun.MainHomeEnglishActivity;
import com.itboye.sunsun.SplashActivity;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.me.ui.LanguageSettingUtil;
import com.itboye.sunsun.me.ui.SwitchLanguageObservable;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.umeng.socialize.utils.Log;

public class LanguageActivity extends BaseActivity {
	RelativeLayout relauyoutEnglish, relauyoutChina, relayoutMoren;
	private Configuration config;
	private DisplayMetrics dm;
	private Resources resources;
	private ImageView imgchina, imgEnglish, imgchinamoren;
	private LanguageSettingUtil languageSetting;
	private TextView switchCh, switchEn;
	private SwitchLanguageObservable obs;
	private RelativeLayout backrela;
	private ImageView back;
	@Override
	@SuppressLint("InlinedApi")
	protected void onCreate(final Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_language);
		App app = (App) getApplication();
		languageSetting = app.getLanguageSetting();// 检查是否已经初始化
		obs = app.getSwitchLangObs();



		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		if(SPUtils.get(App.ctx, null, SPContants.LANGUAGE, "").equals(
				"")){
			SPUtils.put(App.ctx, null, SPContants.LANGUAGE,
					"");
			languageSetting.saveLanguage("");
			LanguageSettingUtil.get().refreshLanguage();// 刷新
			obs.notifyObservers();
			updateActivity();
			imgchinamoren.setVisibility(View.VISIBLE);
			imgchina.setVisibility(View.GONE);
			imgchinamoren.setVisibility(View.GONE);
		}else if(SPUtils.get(App.ctx, null, SPContants.LANGUAGE, "").equals(
				"chinese")){
			imgchinamoren.setVisibility(View.GONE);
			imgchina.setVisibility(View.VISIBLE);
			imgchinamoren.setVisibility(View.GONE);
			languageSetting.saveLanguage("zh");
			LanguageSettingUtil.get().refreshLanguage();// 刷新
			
		}else if(SPUtils.get(App.ctx, null, SPContants.LANGUAGE, "").equals(
				"english")){
			imgchinamoren.setVisibility(View.GONE);
			imgchina.setVisibility(View.GONE);
			imgEnglish.setVisibility(View.VISIBLE);
			languageSetting.saveLanguage("en");
			LanguageSettingUtil.get().refreshLanguage();// 刷新
		}


		findViewById(R.id.relayoutMoren).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						SPUtils.put(App.ctx, null, SPContants.LANGUAGE,
								"chinese");
						languageSetting.saveLanguage("zh");
						LanguageSettingUtil.get().refreshLanguage();// 刷新
						obs.notifyObservers();
						updateActivity();
					}
				});
		findViewById(R.id.relauyoutChina).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// ByAlert.alert("chinese");
						SPUtils.put(App.ctx, null, SPContants.LANGUAGE,
								"chinese");
						languageSetting.saveLanguage("zh");
						LanguageSettingUtil.get().refreshLanguage();// 刷新
						obs.notifyObservers();

						updateActivity();
					}
				});
		findViewById(R.id.relauyoutEnglish).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ByAlert.alert("en");
						SPUtils.put(App.ctx, null, SPContants.LANGUAGE,
								"english");
						languageSetting.saveLanguage("en");
						LanguageSettingUtil.get().refreshLanguage();// 刷新
						obs.notifyObservers();
						imgchina.setVisibility(View.GONE);
						imgEnglish.setVisibility(View.VISIBLE);
						updateActivityEnglish();
					}
				});
		setStatusBarColor(R.color.home_blue);
	}

	private void updateActivity() {
		// TODO Auto-generated method stub
		for (int i = 0; i < App.ctx.activityList.size(); i++) {
			if (App.ctx.activityList.get(i).get() != null) {
				if (App.ctx.activityList.get(i).get()
						.equals("com.itboye.sunsun.person.ui.LanguageActivity")) {
				} else {
					App.ctx.activityList.get(i).get().finish();
				}
			}
		}
		imgchina.setVisibility(View.VISIBLE);
		Intent intent = new Intent();
		intent.setClass(this, SplashActivity.class);// 当前Activity重新打开
		// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		App.ctx.exit();// 遍历所有activity 并finish
	}

	private void updateActivityEnglish() {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(this, SplashActivity.class);// 当前Activity重新打开
		// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		App.ctx.exit();// 遍历所有activity 并finish
	}

	private void shiftLanguage(String sta) {
		// TODO Auto-generated method stub

		if (sta.equals("zh")) {
			Locale.setDefault(Locale.ENGLISH);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.ENGLISH;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());
			updateActivity();
		} else {
			Locale.setDefault(Locale.CHINA);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.CHINA;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());
			updateActivity();
		}
	}

	private boolean isZh() {
		Locale locale = getResources().getConfiguration().locale;
		String language = locale.getLanguage();
		if (language.endsWith("zh"))
			return true;
		else
			return false;
	}

}
